
NO_COLOR=\033[0m
OK_COLOR=\033[32;01m
ERROR_COLOR=\033[31;01m

all: build
build: clean prepare build-go
check: checkConfFileExists checkRegisterService checkLogDirExists
install: backup gitpull gitpullsuccess clean prepare  build-go restart

# --no-print-directory 如果一个make领命令调用另一个默认会打印目录信息，进入某某目录，离开某某目录，但是加这个就不打印了
# todo 只要shell失败就退出，不执行后面语句可以参考git pull 定义一个变量那种方式，同时shell如果出错返回不能是0

gitpull:
	@printf "$(OK_COLOR) 开始拉取代码...$(NO_COLOR)\n"
	@git pull || (echo -e "$(ERROR_COLOR) git pull 失败，详细错误请看上面的输出$(NO_COLOR)" && exit 1)
	@$(eval GIT_PULL_SUCCESS=1)  # 设置变量为1，表示Git pull成功
# 如果Git pull失败，则直接退出整个Makefile执行
ifeq ($(GIT_PULL_SUCCESS),0)
$(error Git pull failed. Exiting.)
endif

count:
	@chmod 755 tools/count.sh
	@tools/count.sh

# 发现了一个如果调用shell后面不在继续跟命令，错误的话可以直接退出
gitpullsuccess:
	@printf "$(OK_COLOR) 拉取代码成功...$(NO_COLOR)\n\n\n"
	@printf ""

backup:
	@printf "$(OK_COLOR)开始备份...$(NO_COLOR)\n"
	@chmod 755 tools/backup.sh
	@tools/backup.sh

rollbackback:
	@printf "$(OK_COLOR)开始恢复到正常...$(NO_COLOR)\n"
	@chmod 755 tools/rollbackback.sh
	@tools/rollbackback.sh


rollback2:
	@printf "$(OK_COLOR)开始回滚...$(NO_COLOR)\n"
	@chmod 755 tools/rollbackn.sh
	@tools/rollbackn.sh 2

rollback3:
	@printf "$(OK_COLOR)开始回滚...$(NO_COLOR)\n"
	@chmod 755 tools/rollbackn.sh
	@tools/rollbackn.sh 3
rollback4:
	@printf "$(OK_COLOR)开始回滚...$(NO_COLOR)\n"
	@chmod 755 tools/rollbackn.sh
	@tools/rollbackn.sh 4
rollback5:
	@printf "$(OK_COLOR)开始回滚...$(NO_COLOR)\n"
	@chmod 755 tools/rollbackn.sh
	@tools/rollbackn.sh 5
rollback6:
	@printf "$(OK_COLOR)开始回滚...$(NO_COLOR)\n"
	@chmod 755 tools/rollbackn.sh
	@tools/rollbackn.sh 6
rollback7:
	@printf "$(OK_COLOR)开始回滚...$(NO_COLOR)\n"
	@chmod 755 tools/rollbackn.sh
	@tools/rollbackn.sh 7
rollback8:
	@printf "$(OK_COLOR)开始回滚...$(NO_COLOR)\n"
	@chmod 755 tools/rollbackn.sh
	@tools/rollbackn.sh 8
rollback9:
	@printf "$(OK_COLOR)开始回滚...$(NO_COLOR)\n"
	@chmod 755 tools/rollbackn.sh
	@tools/rollbackn.sh 9
rollback10:
	@printf "$(OK_COLOR)开始回滚...$(NO_COLOR)\n"
	@chmod 755 tools/rollbackn.sh
	@tools/rollbackn.sh 10

rollback:
	@printf "$(OK_COLOR)开始回滚...$(NO_COLOR)\n"
	@chmod 755 tools/rollback.sh
	@tools/rollback.sh
rollbacki:
	@printf "$(OK_COLOR)开始回滚...$(NO_COLOR)\n"
	@chmod 755 tools/rollbacki.sh
	@tools/rollbacki.sh

backuplist:
	@printf "$(OK_COLOR)备份列表:$(NO_COLOR)\n"
	@chmod 755 tools/backuplist.sh
	@tools/backuplist.sh

sysstop:
	@printf "调用sys的stop会停止守护进程服务，如果stock进程被意外杀死将不会被自动拉起"
	@chmod 755 tools/sysstop.sh
	tools/sysstop.sh

sysrestart:
	@printf "调用sys的restart会有服务的短暂不可用，如果想平滑请用restart"
	@chmod 755 tools/sysrestart.sh
	tools/sysrestart.sh

sysstatus:
	@printf "查看服务autostart状态"
	@chmod 755 tools/sysstatus.sh
	tools/sysstatus.sh

status:
	@printf "查看服务状态"
	@chmod 755 tools/status.sh
	tools/status.sh

restart:
	@printf "\n🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟩 $(OK_COLOR)开始重新启动 平滑重启$(NO_COLOR) 🟩🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦\n"
	@chmod 755 tools/restart.sh
	tools/restart.sh


stop:
	@printf "\n🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟩 $(OK_COLOR) 开始kill -TERM $(NO_COLOR) 🟩🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦\n"
	@printf "$(OK_COLOR)判断是不是有进程...$(NO_COLOR)\n"
	@chmod 755 tools/stop.sh
	tools/stop.sh

start:
	@printf "$(OK_COLOR) 请执行restart代替当前命令 $(NO_COLOR) 🟩🟦🟦🟦🟦🟦🟦🟦🟦🟦\n"

cleantestlog:
	@printf "$(OK_COLOR)开始清理单元测试日志...$(NO_COLOR)\n"
	rm -rf logic/log
	rm -rf dao/log
	rm -rf model/log
	@printf "清理单元测试日志完成\n\n"


build-go:
	@chmod 755 tools/build-go.sh
	@./tools/build-go.sh

buildgo2:
	@chmod 755 tools/build-go2.sh
	@./tools/build-go2.sh

createConfFile:
	@chmod 755 ./tools/create_conf_file.sh
	@./tools/create_conf_file.sh

package:
	go mod vendor

prepare:
	@printf "$(OK_COLOR)开始编译...$(NO_COLOR)\n\n"

clean:
	@printf "$(OK_COLOR)开始清理以前文件...$(NO_COLOR)\n"
	rm -rf $(APP_NAME)
	@printf "清理代码完成\n\n"

checkConfFileExists:
	@printf "$(OK_COLOR)开始检查配置文件是否创建...$(NO_COLOR)\n"
	@chmod 755 tools/check_configfile_exists.sh
	@tools/check_configfile_exists.sh
	@printf "检查配置文件完成\n\n"

checkRegisterService:
	@printf "$(OK_COLOR)开始检查是否注册服务$(NO_COLOR)\n"
	@chmod 755 tools/check_register_service.sh
	./tools/check_register_service.sh
	@printf "检查service完成\n\n"

checkLogDirExists:
	@printf "$(OK_COLOR)开始检查log目录是否创建好软连...$(NO_COLOR)\n"
	@chmod 755 tools/check_log_dir.sh
	./tools/check_log_dir.sh
	@printf "检查service完成\n\n"

taillog:
	tail -f log/stock.log

buildgo:
	echo "go build -o stock main.go"
	go build -o "stock" main.go

.ONESHELL:
